package com.prp.algorithm;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import com.prp.db.DAOFactory;
import com.prp.db.idao.ISubjectCategoryDAO;
import com.prp.db.idao.ISubjectChildCategoryDAO;
import com.prp.db.idao.ISubjectRecommendationDAO;
import com.prp.db.po.SubjectCategory;
import com.prp.db.po.SubjectChildCategory;
import com.prp.db.po.SubjectRecommendation;

public class SubjectsRelatedRecommend {

	public static void main(String[] args){
		System.out.println("///////////////////////////////////////");
		ArrayList<SubjectRecommendation> list = getSubjectRecommend(10,1);
		System.out.println( list.size());
		for( int i = 0; i < list.size(); i++){
			SubjectRecommendation s = list.get(i);
			System.out.println(i+" : "+s.getRecommendationId());
		}
	}
	/**
	 * 返回对用户userId的topNum主题贴推荐
	 * 
	 * @param topNum
	 * @param userId
	 * @return
	 */
	public synchronized static ArrayList<SubjectRecommendation> getSubjectRecommend(
			int topNum, int userId) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		ISubjectRecommendationDAO subRecDAO = factory
				.getSubjectRecommendationDAO();
		//SubjectRecommendation对应数据库主题帖表格
		ArrayList<SubjectRecommendation> subRecList = (ArrayList<SubjectRecommendation>) subRecDAO
				.findAll();
		
		//RecommendationRelation算法使用的类
		ArrayList<RecommendationRelation> rrList = new ArrayList<RecommendationRelation>();
		for (int i = 0; i < subRecList.size(); i++) {
			RecommendationRelation tempRelation = new RecommendationRelation();
			tempRelation.index = i;
			tempRelation.clickValue = subRecList.get(i).getClickFreq();
			tempRelation.waitingValue = subRecList.get(i).getWaitingTime();
			tempRelation.replyValue = subRecList.get(i).getReplyNum();
			rrList.add(tempRelation);
		}

		rrList = SubjectsRecommend.getSortRelation(rrList);

		ISubjectCategoryDAO subcDAO = factory.getSubjectCategoryDAO();
		//SubjectCategory对应主题类别表，SubjectChildCategory对应主题子类别
		ArrayList<SubjectCategory> subcList = (ArrayList<SubjectCategory>) subcDAO
				.findAll();

		ISubjectChildCategoryDAO subccDAO = factory
				.getSubjectChildCategoryDAO();
		ArrayList<SubjectChildCategory> subccList = (ArrayList<SubjectChildCategory>) subccDAO
				.findAll();

		UserInfo user = new UserInfo();
		user.user_id = userId;

		for (int i = 0; i < subcList.size(); i++)
			user.mark_list.add(0.00);
		for (int i = 0; i < subccList.size(); i++)
			user.detail_mark_list.add(0.00);
		
		NeighboursRecommend.calSearchInfo(factory, subcList, subccList, user);
		NeighboursRecommend.calSubjectPublish(factory, subcList, subccList,
				user);
		NeighboursRecommend.calSubjectRecommendation(factory, subcList,
				subccList, user);
		NeighboursRecommend.calSubjectComment(factory, subcList, subccList,
				user);

		rrList = sortByRelate(rrList, subRecList, subccList, user);

		ArrayList<SubjectRecommendation> resultList = new ArrayList<SubjectRecommendation>();
		for (int i = 0; i < rrList.size() && i < topNum; i++)
			resultList.add(subRecList.get(rrList.get(i).index));
		return resultList;
	}

	public synchronized static ArrayList<RecommendationRelation> sortByRelate(
			ArrayList<RecommendationRelation> rrList,
			ArrayList<SubjectRecommendation> subRecList,
			ArrayList<SubjectChildCategory> subccList, UserInfo user) {
		for (int i = 0; i < rrList.size(); i++) {
			int index = -1, subccId = subRecList.get(rrList.get(i).index)
					.getCategoryChildId();
			for (index = 0; index < subccList.size(); index++) {
				if (subccList.get(index).getChildCategoryId() == subccId)
					break;
			}
			rrList.get(i).value = rrList.get(i).value * user.detail_mark_list.get(index);
		}
		Collections.sort(rrList, new Comparator<RecommendationRelation>() {
			public int compare(RecommendationRelation o1,
					RecommendationRelation o2) {
				return (o1.value < o2.value) ? 1 : ((o1.value > o2.value) ? -1
						: 0);
			}
		});
		return rrList;
	}
}
